package com.letoken.platform.pub.configuration;

import com.amazonaws.ClientConfiguration;
import com.amazonaws.Protocol;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CreateBucketRequest;
import com.amazonaws.services.s3.transfer.TransferManager;
import com.amazonaws.services.s3.transfer.TransferManagerBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;

/**
 * @author wyl
 * @Description
 * @date 2022/12/27 17:31
 * @Version 1.0
 */
@Configuration
@EnableConfigurationProperties({AwsS3Configuration.class})
public class AmazonS3Configuration {
    @Resource
    private AwsS3Configuration awsS3Configuration;

    @Bean(name = "amazonS3")
    public AmazonS3 getAmazonS3() {
        String accessKey = awsS3Configuration.getAccessKey();
        String secretKey = awsS3Configuration.getSecretKey();
        AWSCredentials credentials = new BasicAWSCredentials(accessKey, secretKey);
        ClientConfiguration conf = new ClientConfiguration();
        // 设置AmazonS3使用的最大连接数
        conf.setMaxConnections(awsS3Configuration.getAmazonS3MaxConnections());
        // 设置socket超时时间
        conf.setSocketTimeout(awsS3Configuration.getAmazonS3SocketTimeout());
        // 设置失败请求重试次数
        conf.setMaxErrorRetry(awsS3Configuration.getAmazonS3MaxErrorRetry());
        // 设置协议
        if (!"blank".equals(awsS3Configuration.getAmazonS3Protocol())) {
            switch (awsS3Configuration.getAmazonS3Protocol()) {
                case "https":
                    conf.setProtocol(Protocol.HTTPS);
                    break;
                case "http":
                    conf.setProtocol(Protocol.HTTP);
                    break;
                default:
                    break;
            }
        }
        AmazonS3 s3Client = AmazonS3ClientBuilder.standard()
                .withCredentials(new AWSStaticCredentialsProvider(credentials))
                .withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(awsS3Configuration.getHosts().get(0), awsS3Configuration.getRegion()))
                .withClientConfiguration(conf)
                .build();
//        checkAndCreateBucket(s3Client);
        return s3Client;
    }


    @Bean(name = "transferManager")
    public TransferManager getTransferManager() {
        return TransferManagerBuilder.standard().withS3Client(getAmazonS3()).build();
    }


    /**
     * 检查桶是否存在，不存在则创建创建
     *
     * @param s3Client
     */
    private void checkAndCreateBucket(AmazonS3 s3Client) {
        boolean exists = s3Client.doesBucketExistV2(awsS3Configuration.getBucketName());
        if (!exists) {
            CreateBucketRequest request = new CreateBucketRequest(awsS3Configuration.getBucketName());
            s3Client.createBucket(request);
        }
    }
}
